package com.xuef.controller;

import com.google.common.collect.Lists;
import com.xuef.entity.EsBlog;
import com.xuef.entity.User;
import com.xuef.service.EsBlogService;
import com.xuef.service.IUserService;
import com.xuef.vo.TagVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by moveb on 2018/5/13.
 */
@Controller
@RequestMapping("/blogs")
public class BlogController {
    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private EsBlogService esBlogService;
    @Autowired
    private IUserService userService;

    @GetMapping
    public String listEsBlogs(
            @RequestParam(value = "order", required = false, defaultValue = "new") String order,
            @RequestParam(value = "keyword", required = false, defaultValue = "blog") String keyword,
            @RequestParam(value = "async", required = false) boolean async,
            @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            Model model,
            Authentication authentication) {

        Page<EsBlog> page = null;
        List<EsBlog> list = null;

        // 如果关键词不为空，就进行全局搜索
        if(!keyword.equals("")){
            Pageable pageable = new PageRequest(pageIndex, pageSize);
            page = esBlogService.fullTextSearch(keyword,keyword,keyword,keyword,pageable);
        }
        if(page != null){
            list = page.getContent();
        }

        List<TagVO> tags = esBlogService.listTop30Tags(null);
        List<TagVO> newTags = Lists.newArrayList();
        // 去除单字符标签
        for(TagVO tag : tags){
            if(tag.getName().length() > 1){
                newTags.add(tag);
//                logger.info(tag.toString());
            }
        }

        // 所有用户
        List<User> userList = userService.listUsers();

        // 携带数据到指定页面
        if(authentication != null && authentication.isAuthenticated()) {
            model.addAttribute("username_authenticated", authentication.getName());
        }else{
            model.addAttribute("username_authenticated", "nil");
        }
        model.addAttribute("order", order);
        model.addAttribute("keyword", keyword);
        model.addAttribute("page", page);
        model.addAttribute("blogList", list);
        model.addAttribute("tags", newTags);
//        List<User> users = esBlogService.listTop12Users();
        model.addAttribute("userList", userList);

        return (async == true ? "index :: #mainContainerRepleace" : "index");
    }
}
